<template>
  <div class="h-full w-full flex flex-col justify-between">
    <div class="form px-[15px] pt-[15px] border-bottom">
      <el-form>
        <el-row :gutter="12">
          <el-col :span="3">
            <el-form-item>
              <el-input v-model="form.issue" placeholder="游戏期号" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item>
              <el-select class="!w-full" v-model="form.lottery_id">
                <el-option label="全部游戏" value="-1" />
                <el-option
                  v-for="item in gameTotal"
                  :key="item.LotteryId"
                  :label="item.Name"
                  :value="item.LotteryId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item>
              <el-button type="primary" @click.stop="search">搜索</el-button>
              <el-button type="danger" @click.stop="handleRefresh">清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="grow-[1] h-0 mx-[15px] mt-[10px]">
      <el-table
        scrollbar-always-on
        ref="elTableRef"
        v-loading="state.loading"
        :data="list"
        style="width: 100%"
        height="100%"
        border
        stripe
        fit
      >
        <template #empty>
          <ui-empty />
        </template>

        <el-table-column
          align="center"
          prop="UserName"
          label="用户名"
          show-overflow-tooltip
          min-width="80"
        >
          <template #default="{ row }: TableItem">
            {{ row.UserName || "--" }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="NickName"
          label="昵称"
          show-overflow-tooltip
          min-width="80"
        >
          <template #default="{ row }: TableItem">
            {{ row.NickName || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Note" label="备注" show-overflow-tooltip>
          <template #default="{ row }: TableItem">
            {{ row.Note || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Name" label="游戏" show-overflow-tooltip>
          <template #default="{ row }: TableItem">
            {{ gameName(row.LotteryId) || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="BitchNo" label="期号" min-width="80">
          <template #default="{ row }: TableItem">
            {{ row.BitchNo || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Cat1" label="玩法" show-overflow-tooltip>
          <template #default="{ row }: TableItem">
            {{ row.Cat1 || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Cat2" label="下注内容" show-overflow-tooltip>
          <template #default="{ row }: TableItem"> {{ row.Cat2 || "-" }}-{{ row.Cat3 }} </template>
        </el-table-column>
        <el-table-column prop="TotalAmount" align="center" label="下注金额" min-width="80">
          <template #default="{ row }: TableItem">
            <ui-amount :num="row.TotalAmount"></ui-amount>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="CreatedAt" label="下注时间" width="170">
          <template #default="{ row }: TableItem">
            {{ formatDate(row.CreatedAt) }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="flex justify-between my-[10px] mx-[15px]">
      <div class="flex-1 w-0 mr-[20px] flex justify-start">
        <div>
          <el-alert
            title="注意：报表最多保留两月数据。"
            type="warning"
            show-icon
            :closable="false"
          />
        </div>
      </div>
      <ui-pagination
        :total="state.total"
        v-model:pageSize="state.pageSize"
        v-model:currentPage="state.page"
        @current-change="getData"
      ></ui-pagination>
    </div>
  </div>
</template>
<script setup lang="ts">
import { useList } from "@/hooks/useList";
import { reqReportUnsettled } from "@/api/report";
import { useGameStore } from "@/store/modules/game";
import { storeToRefs } from "pinia";
import { formatDate } from "@/utils/common";

interface TableItem {
  row: ReportUnsettledMo;
}

const gameStore = useGameStore();
const { gameTotal } = storeToRefs(gameStore);

const form = reactive({
  lottery_id: "-1",
  issue: ""
});

const gameName = (id: number) => {
  return gameTotal.value.find(item => item.LotteryId == id)?.Name || "--";
};

const getParams = () => {
  const params: any = {};
  if (form.issue) {
    params.Issue = form.issue;
  }
  if (form.lottery_id && form.lottery_id != "-1") {
    params.LotteryId = form.lottery_id;
  }
  return params;
};
const { elTableRef, state, list, getData, search } = useList<ReportUnsettledMo, any>(
  reqReportUnsettled,
  getParams
);
// 重置
const handleRefresh = () => {
  form.lottery_id = "-1";
  form.issue = "";
  search();
};

onMounted(() => {
  getData();
});
</script>
<style lang="scss" scoped></style>
